! Program to identify the atom type, bond list/type, angle list/type, 
! dihedral list/type of molecules
program main
use molecule
implicit none
   !----------------------------------------------------------------------------
   integer            :: moltype, infotype, ioerr
   logical            :: lappend, first = .true.
   character (len=80) :: append, oneline, outfile
   !----------------------------------------------------------------------------
   !
   ! main menu for molecules
   write(*,'(/,10x,"Please select your molecule:")')
   write(*,'(10x,"===========================================================================")')
   write(*,'(15x," 1. Liquid crystal 5CB;                  2. Butene olefin;")')
   write(*,'(15x," 3. Hexene olefin;                       4. Octene olefin;")')
   write(*,'(15x," 5. Decene olefin;                       6. Dodecene olefin;")')
   write(*,'(15x," 7. Water (H2O);                         8. Methane (CH4);")')
   write(*,'(15x," 9. Ethylene (H2C=CH2);                 10. Beta-hexene olefin;")')
   write(*,'(15x,"11. Gamma-hexene olefin;                12. Hydrogenated hexene dimer;")')
   write(*,'(15x,"13. Oleic acid;                         14. Dye from FCL;")')
   write(*,'(15x,"15. Nafion;                             16. Linear hydrocarbon;")')
   write(*,'(15x,"17. PEO;                                18. Read from file;")')
   write(*,'(15x," 0. Exit.")')
   write(*,'(10x,"---------------------------------------------------------------------------")')
   write(*,'(10x,"Your choice [1]: ",$)')
   read(*,'(A)', iostat=ioerr) oneline
   moltype = 1
   if ( ioerr.eq.0.and.oneline.ne.'' ) then
      read(oneline, *, iostat=ioerr) moltype
      if (ioerr.ne.0) moltype = 0
   endif
   write(*,'(10x,"===========================================================================")')
   !
   ! initialization 
   select case ( moltype )
   case ( 1 ) 
      call lc5cb_init()
   case ( 2 )
      call butene_init()
   case ( 3 )
      call hexene_init()
   case ( 4 )
      call octene_init()
   case ( 5 )
      call decene_init()
   case ( 6 )
      call dodecene_init()
   case ( 7 )
      call water_init()
   case ( 8 )
      call methane_init()
   case ( 9 )
      call ethylene_init()
   case ( 10 )
      call beta_hexene_init()
   case ( 11 )
      call gamma_hexene_init()
   case ( 12 )
      call hexene_dimer_init()
   case ( 13 )
      call oleic_acid_init()
   case ( 14 )
      call dye_init()
   case ( 15 )
      call nafion_init()
   case ( 16 )
      call hydrocarbon_init()
   case ( 17 )
      call peo_init()
   case ( 18 )
      call readmol_init()
   case default
      stop
   end select
   !
   ! identify atom types, bonds, angles, dihedrals
   call identify()
   !
   ! display identified info
   call disp(0)
   !
   append = 'rewind'
   outfile = trim(molname)
   !
   ! main menu for output
   do while (.true.)
      write(*,'(/,10x,"===========================================================================")')
      write(*,'(10x,"Please select the info you want to display/modify/write:")')
      write(*,'(10x,"---------------------------------------------------------------------------")')
      write(*,'(15x,"1. atom list and type;                   2. bond list and type;")')
      write(*,'(15x,"3. angle list and type;                  4. dihedral list and type;")')
      write(*,'(15x,"5. write all info;                       6. write in xyz;")')
      write(*,'(10x,"---------------------------------------------------------------------------")')
      write(*,'(15x,"0. Exit.")')
      write(*,'(10x,"Your choice [0]: ",$)')
      read(*,'(A)', iostat=ioerr) oneline
      infotype = 0
      if (ioerr.eq.0.and.oneline.ne.'') then
         read(oneline,*,iostat=ioerr) infotype
         if ( ioerr.ne.0 ) infotype=0
      endif
      if ( infotype.le.0.or.infotype.gt.6 ) exit
      call disp( infotype )
      !
      if ( infotype.lt.5 ) then
         write(*,'(/,10x,"Would you like to modify the type/ID pair? (y/n)[n]: ",$)')
         read(*,'(A)', iostat=ioerr) oneline
         if (ioerr.eq.0.and.(oneline.eq.'y'.or.oneline.eq.'Y')) then
            call modify(infotype)
            call disp( infotype )
         endif
         !
         write(*,'(/,10x,"Write the related info now? (y/n) [n]: ",$)')
         read(*,'(A)', iostat=ioerr) oneline
         if (ioerr.ne.0.or.(oneline.ne.'y'.and.oneline.ne.'Y')) then
            write(*,'(10x,"===========================================================================")')
            cycle
         endif
      endif
      !
      write(*,'(10x,"Please input the prefix of the output file[",A,"]: ",$)') trim(outfile)
      read(*,'(A)', iostat=ioerr) oneline
      write(*,'(10x,"===========================================================================")')
      if (ioerr.eq.0.and.oneline.ne.'') outfile = trim(oneline)
      if (infotype == 6) append = 'rewind'
      call writeinfo(outfile, append, infotype)
      !
      append = 'append'
   enddo
   !
end program
